package com.training.bt;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.training.bt.tracker.CustomTracker;

@SpringBootApplication
@EnableScheduling
@MapperScan("com.training.bt.mapper")
public class BtApplication {

    private static final Logger logger = LoggerFactory.getLogger(BtApplication.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(BtApplication.class, args);

        try {
            // 1. 启动Tracker服务器
            startTracker(ctx);
            
            // 2. 自动执行种子重新注册（通过CommandLineRunner）
            // 不需要手动调用，Spring会自动执行所有CommandLineRunner
            
            logger.info("应用启动完成");
        } catch (Exception e) {
            logger.error("启动过程中发生错误", e);
        }
    }

    private static void startTracker(ConfigurableApplicationContext ctx) {
        try {
            CustomTracker tracker = ctx.getBean(CustomTracker.class);
            tracker.start();
            logger.info("Tracker 已启动: {}", tracker.getAnnounceUrl());
        } catch (Exception e) {
            logger.error("Tracker 启动失败", e);
        }
    }
}